﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Cosmetic.Logic
{
    public class articleManger : LogicBase
    {
        public void Insert(article  aProduct)
        {
            m_CosmeticDC.article.InsertOnSubmit(aProduct); 
            m_CosmeticDC.SubmitChanges();
        }
        public void  inserts( help ahelp)
        {
            m_CosmeticDC.help .InsertOnSubmit(ahelp );
            m_CosmeticDC.SubmitChanges();
        }
        public void deletes( help ahelp)
        {
            m_CosmeticDC.help.DeleteOnSubmit(ahelp );
            m_CosmeticDC.SubmitChanges();
        }

        public help  GetProductByIdhelp(int aProductId)
        {
            return m_CosmeticDC.help .Single(theProect => theProect.id == aProductId);
        }


        public IEnumerable GetAllhelp()
        {
            var result = from theActivity in m_CosmeticDC.help 
                         join thenewclass in m_CosmeticDC.helpclass
                         on theActivity.classid  equals thenewclass.id
                         select new
                         {
                             contents = theActivity.contents,
                             name = thenewclass.classname,
                             id = theActivity.id,
                             title=theActivity .title 
                         };
            return result;
        }
        public List<help > GetSubCateogoriesBySupCategory(helpclass  aSupCategory)
        {
            try
            {
                return GetSubCateogoriesBySupCategory(aSupCategory.id   );
            }
            catch
            {
                return null;
            }
        }

        public List<help > GetSubCateogoriesBySupCategory(int aSupCategoryId)
        {
            var result = (from theCategory in m_CosmeticDC.help
                          where theCategory.classid == aSupCategoryId 
                          select theCategory).ToList();
            return result;
        }





        /// <summary>
        /// Delete product
        /// </summary>
        /// <param name="aProduct">The product want delete</param>
        public void Delete(article  aProduct)
        {

            m_CosmeticDC.article.DeleteOnSubmit(aProduct);
            m_CosmeticDC.SubmitChanges();
        }
        public article GetProductById(int aProductId)
        {
            return m_CosmeticDC.article.Single(theProect => theProect.id  == aProductId);
        }


        public IEnumerable GetAll()
        {
            var result = from theActivity in m_CosmeticDC.article 
                    join thenewclass in m_CosmeticDC.articleclass
                    on theActivity.classid equals thenewclass.id 
                    orderby theActivity.createTime  descending
                         select new
                         {
                           contents=theActivity .contents ,
                             name = thenewclass .name ,
                             id=theActivity .id ,
                             count=theActivity .count ,
                             title=theActivity.tilte
                         };
            return result;
        }

        public List<article > getall1()
        {
            var result = (from theProduct in m_CosmeticDC.article
                          orderby theProduct.createTime  descending
                          select theProduct).ToList();
            return result;
        }
      
        public List<article> GetAlls( int id)
        {
            var result = from theActivity in m_CosmeticDC.article  where theActivity.classid  ==id 
                         orderby theActivity.createTime descending
                         select theActivity;
            return result.ToList();
        }

        public IEnumerable GetALL1()
        {
            var result = from thePlate in m_CosmeticDC.article
                         orderby thePlate.createTime descending
                         select thePlate;
            return result.Take(5);
        }


        public IEnumerable GetAlls01(int id )
        {
            var result = from thePlate in m_CosmeticDC.article where thePlate .classid ==id 
                         orderby thePlate.createTime descending
                         select thePlate;
            return result.Take(6);
        }


        public static DataTable GetProductInfoByIds(int aProductId)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);

            try
            {
                con.Open();
                string cmdStr = string.Empty;
                cmdStr = "select classid,contents,createTime,tilte,count from article where id='" + aProductId + "'";
                SqlCommand com = new SqlCommand(cmdStr, con);
                SqlDataAdapter adap = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                return ds.Tables[0];
            }
            finally
            {
                con.Close();
            }
        }



    }
}
